package com.atguigu.yygh.hosp.controller;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author yjj
 * @date 2021年09月02日23:14
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/yygh/hospitalSet")
@CrossOrigin //允许跨域访问
public class HospitalController {
    //在IService中也做了curd封装
    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private HospitalService hospitalService;

    /**
     * 查询出回医院设置表中所有数据
     *
     * @param
     * @return null
     * @author yjj
     * @date 2021/9/2 23:22
     */
    @ApiOperation(value = "获取所有医院信息")
    @PostMapping("/list")
    public Result getList() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 删除医院设置
     *
     * @param
     * @return null
     * @author yjj
     * @date 2021/9/2 23:40
     */
    @ApiOperation(value = "删除医院信息")
    @DeleteMapping("/delete/{id}")
    public boolean deleteOne(@PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        return b;
    }

    /**
     * 带条件分页查询
     * RequestBody通过json数据来传参数 required = false 意思是这个参数可以为空
     *
     * @param
     * @return null
     * @author yjj
     * @date 2021/9/3 23:12
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("/findPageHospital/{current}/{limit}")
    public Result findPageHospital(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，及每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        //构造条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String name = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            queryWrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }
        //调用方法实现效果分页
        Page<HospitalSet> page1 = hospitalSetService.page(page, queryWrapper);
        return Result.ok(page1);
    }

    /**
     * 添加医院设置
     *
     * @param
     * @return null
     * @author yjj
     * @date 2021/9/4 21:29
     */
    @ApiOperation("添加医院设置")
    @PostMapping("/addHospital")
    public Result addHospital(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    /**
     * 跟根据id获取医院详细信息
     *
     * @param
     * @return null
     * @author yjj
     * @date 2021/9/4 21:39
     */
    @PostMapping("/detailHospital/{id}")
    public Result detailHospital(@PathVariable Integer id) {
      //  try {
      //      int a = 1/0;
      //  } catch (Exception e) {
      //     throw new YyghException("失败",201);
      //  }
        HospitalSet one = hospitalSetService.getById(id);
        return Result.ok(one);
    }
    /**
     * 更新医院数据
     *
     * @param
     * @return null
     * @author yjj
     * @date 2021/9/4 21:36
     */
    @ApiOperation("更新数据")
    @PostMapping("/updateHospital")
    public Result updateHospital(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    /**
     * 批量删除医院的信息逻辑删除
     *
     * @param
     * @return null
     * @author yjj
     * @date 2021/9/4 21:34
     */
    @ApiOperation("批量删除数据")
    @DeleteMapping("/batchHospital")
    public Result batchHospital(@RequestBody(required = false) List<Long> ids) {
        boolean b = hospitalSetService.removeByIds(ids);
        if (b) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //医院设置解锁1和锁定0
    @ApiOperation("锁定")
    @PutMapping("/lock/{id}/{status}")
    public Result lock(@PathVariable Long id,@PathVariable Integer status){
        //根据医院查询设置信息
        HospitalSet byId = hospitalSetService.getById(id);
        //设置状态
        byId.setStatus(status);
        //调用方法
        boolean b = hospitalSetService.updateById(byId);
        if (b){
         return    Result.ok();
        }else {
         return    Result.fail();
        }
    }



    //发送签名密钥
    @ApiOperation("发送密钥")
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        //TODO
        return null;
    }



    @ApiOperation(value = "获取分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Integer page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Integer limit,

            @ApiParam(name = "hospitalQueryVo", value = "查询对象", required = false)
                    HospitalQueryVo hospitalQueryVo) {
        return Result.ok(hospitalService.selectPage(page, limit, hospitalQueryVo));
    }

    //更新医院上线状态，医院信息是存在mangoDB中所以是对mongoDB进行操作
    @ApiOperation("医院上线更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result lock(
            @ApiParam(name = "id", value = "医院id", required = true)
            @PathVariable("id") String id,
            @ApiParam(name = "status", value = "状态（0：未上线 1：已上线）", required = true)
            @PathVariable("status") Integer status){
        hospitalService.updateStatus(id, status);
        return Result.ok();
    }

    @ApiOperation(value = "获取医院详情")
    @GetMapping("show/{id}")
    public Result show(
            @ApiParam(name = "id", value = "医院id", required = true)
            @PathVariable String id) {
        return Result.ok(hospitalService.show(id));
    }

}
